package com.lipeng.service.impl;

import com.lipeng.bean.Attention;
import com.lipeng.bean.Club;
import com.lipeng.bean.Collect;
import com.lipeng.bean.Shop;
import com.lipeng.mapper.AttentionMapper;
import com.lipeng.mapper.ClubMapper;
import com.lipeng.service.ClubService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/7.
 */

@Service
public class ClubServiceImpl implements ClubService {

    @Resource
    private ClubMapper clubMapper;
    @Resource
    private AttentionMapper attentionMapper;

    AjaxResult result = new AjaxResult();


    @Override
    public Club selectClubById(Integer cid) {
        return clubMapper.selectByPrimaryKey(cid);
    }

    @Override
    public List<Club> selectAllClub() {
        return clubMapper.selectAllClub();
    }

    @Override
    public int insertClub(Club club) {
        return clubMapper.insert(club);

    }

    @Override
    public int updateClub(Club club) {
        return clubMapper.updateByPrimaryKey(club);

    }

    @Override
    public int deleteClubById(Integer cid) {
        return clubMapper.deleteByPrimaryKey(cid);

    }

    @Override
    public List<Club> selectClubByName(String name) {
        return clubMapper.selectClubByName(name);

    }

    @Override
    public AjaxResult addAttention(Integer uid, Integer cid) {
        Attention attention = new Attention();
        attention.setUid(uid);
        attention.setCid(cid);
        Integer num = attentionMapper.insert(attention);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("关注失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("关注成功");
        return result;
    }

    @Override
    public AjaxResult delAttention(Integer attid) {
        Integer num = attentionMapper.deleteByPrimaryKey(attid);
        if (num <1){
            result.setCode("0");
            result.setMessage("取消关注失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("取消关注成功");
        return result;
    }

    @Override
    public AjaxResult selectAttentionByUid(Integer uid) {
        List<Attention> attentionList = attentionMapper.selectAttentionByUid(uid);
        List<Club> clubList = new ArrayList<Club>();
        for (Attention attention : attentionList){
            Club club = attention.getClub();
            clubList.add(club);
        }
        for (Club club : clubList){
            Integer count = selectAttentionCountByCid(club.getCid());
            club.setCpic(IPUtils.getIP()+":8080"+club.getCpic());
            club.setAttentionCount(count);
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(clubList);
        return result;
    }

    @Override
    public Integer selectAttentionCountByCid(Integer cid) {
        return attentionMapper.selectAttentionCountByCid(cid);
    }

    @Override
    public Attention selectClubAttention(Integer cid, Integer uid) {
        return attentionMapper.selectClubAttention(cid,uid);
    }
}
